<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="学期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="semesterId">
              <j-dict-select-tag type="list" v-model="model.semesterId" dictCode="sk_semester,semester_name,id,del_flag=0" placeholder="请选择学期" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="开始日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="startDate">
              <!-- <j-date placeholder="请选择开始日期" v-model="model.startDate"   /> -->
              <a-date-picker
                v-model="model.startDate"
                :disabledDate="disabledStartDate"
                format="YYYY-MM-DD"
                value-format="YYYY-MM-DD"
                style="width: 100%"
                placeholder="请选择开始日期"
                @openChange="handleStartOpenChange"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="结束日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endDate">
              <!-- <j-date placeholder="请选择结束日期" v-model="model.endDate"  style="width: 100%" /> -->
              <a-date-picker
                v-model="model.endDate"
                :disabledDate="disabledEndDate"
                format="YYYY-MM-DD"
                value-format="YYYY-MM-DD"
                placeholder="请选择结束日期"
                style="width: 100%"
                :open="endOpen"
                @openChange="handleEndOpenChange"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="第几周" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="weekNum">
              <a-input-number :min="0" v-model="model.weekNum" placeholder="请输入第几周" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="食谱图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="picture">
              <j-image-upload :number="imgMaxNumber" isMultiple   v-model="model.picture" ></j-image-upload>
              <div>最多上传 <span style="color: #f5222d;">{{ imgMaxNumber }}</span> 张食谱图片</div>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import moment from 'moment';
  export default {
    name: 'SkCookbookForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        imgMaxNumber:1,
        endOpen: false,
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           weekNum: [
              { required: true,  message: '请输入第几周！'},
              { pattern: /^-?\d+$/, message: '请输入整数!'},
           ],
           semesterId: [{ required: true, message: '请选择学期！' }],
           name: [{ required: true, message: '请输入家长姓名！' }],
           startDate: [{ required: true, message: '请输入开始日期！' }],
           endDate: [{ required: true, message: '请输入结束日期！' }],
           picture: [{ required: true, message: '请上传食谱图片！' }],
        },
        url: {
          add: "/canteen/skCookbook/add",
          edit: "/canteen/skCookbook/edit",
          queryById: "/canteen/skCookbook/queryById"
        },
        startDate:"",
        endDate:""
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    mounted(){
    },
    methods: {
      disabledStartDate(startValue) {
        const endValue = this.model.endDate;
        if (!startValue || !endValue) {
          return false;
        }
        return startValue.valueOf() > endValue.valueOf();
      },
      disabledEndDate(endValue) {
        const startValue = this.model.startDate;
        if (!endValue || !startValue) {
          return false;
        }
        return startValue.valueOf() >= endValue.valueOf();
      },
      handleStartOpenChange(open) {
        if (!open) {
          this.endOpen = true;
        }
      },
      handleEndOpenChange(open) {
        this.endOpen = open;
      },
      add () {
        this.edit(this.modelDefault);
       
      },
      edit (record) {
        this.model = Object.assign({}, record);
        if (this.model.id) {
          this.model.startDate = moment(record.startDate);
          this.model.endDate = moment(record.endDate);
        }
      },
      submitForm () {
        const that = this;
        this.model.startDate = moment(this.model.startDate).format('YYYY-MM-DD');
        this.model.endDate = moment(this.model.endDate).format('YYYY-MM-DD');
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            this.$emit("modelLoading",that.confirmLoading)
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              this.$emit("modelLoading",that.confirmLoading)
            })
          }
         
        })
      },
    }
  }
</script>